<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\Admin\Setting;
use App\Models\Api\Common\Sms;
use App\Models\Common;
use App\Models\Member;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    public function login(Request $request)
    {
        $Model = new User;
        $post = $Model->check($request,'applogin');
        if(Common::is_phone($post['phone'])){
            $user = $Model->where('phone', $post['phone'])->first();
        }else{
            $user = $Model->where('username', $post['phone'])->first();
        }
        if($user){
            if(Hash::check($post['password'],$user['password'])){
                Common::resJson(['code'=>631,'data'=>$user->api_login(),'msg'=>'登录成功']);
            }else{
                Common::resJson(['code'=>632,'data'=>'','msg'=>'密码错误']);
            }
        }else{
            Common::resJson(['code'=>633,'data'=>'','msg'=>'手机号码不存在']);
        }
    }

    public function smslogin(Request $request){
        $phone = Sms::check($request,true);
        if($phone){
            $Model = new User;
            $user = $Model->where('phone', $phone)->first();
            if($user){
                Common::resJson(['code'=>631,'data'=>$user->api_login(),'msg'=>'登录成功']);
            }else{
                $user = $Model->api_reg('phone',$phone);
                Common::resJson([
                    'code'=> 621,
                    'msg' => '注册成功',
                    'data' => $user->api_user_data()
                ]);
            }
        }
    }

    public function smspw(Request $request){
        $phone = Sms::check($request,true);
        if($phone){
            $Model = new User;
            $user = $Model->where('phone', $phone)->first();
            if($user){
                $password = $request->input('password','');
                if($password){
                    $user->api_changepw($password);
                    Common::resJson(['code'=>641,'data'=>'','msg'=>'修改成功']);
                }else{
                    Common::resJson(['code'=>643,'data'=>'','msg'=>'缺少密码']);
                }
            }else{
                Common::resJson(['code'=>642,'data'=>'','msg'=>'无效手机号']);
            }
        }
    }

    public function logout(Request $request)
    {
        $request->api_user->logout();
        Common::resJson(['code'=>641,'data'=>'','msg'=>'退出成功']);
    }

    public function register(Request $request)
    {
        $userModel = new User;
        $post = $userModel->check($request,'register');
        $user = $userModel->register($post);
        Common::resJson([
            'code'=> 621,
            'msg' => '注册成功',
            'data' => $user->api_user_data()
        ]);
    }

    public function avatar(Request $request)
    {
        $file = $request->file('avatar');
        $avatar = Common::uploadFile($file,'avatar');
        if($avatar){
            $user = $request->api_user;
            $oldavatar = $user->avatar;
            $user->avatar = $avatar;
            if($user->save()){
                $user->delAvatar($oldavatar);
                Common::resJson(['code'=>701,'data'=>Common::filePath($user->avatar),'msg'=>'上传成功']);
            }else{
                Common::resJson(['code'=>705,'data'=>'','msg'=>'保存错误']);
            }
        }
        Common::resJson(['code'=>704,'data'=>'','msg'=>'上传错误']);
    }

    public function del(Request $request)
    {
        $user = User::find($request->input('id'));
        if($user){
            if($user->delete()){
                return [
                    'code'=> 1,
                    'msg' => '删除成功',
                    'data' => []
                ];
            }
        }
        return [
            'code'=> 0,
            'msg' => '删除失败',
            'data' => []
        ];
    }
}
